package com.example.reagent_management.ui.operatedialog.dialogs

import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.example.lib_base.adapter.ItemChildCLickListener
import com.example.lib_base.adapter.ItemClickListener
import com.example.lib_base.base.BaseDBDialogFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.lib_base.utils.onQueryTextFlow
import com.example.reagent_management.BR
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentReagentManagerBinding
import com.example.reagent_management.room.entity.ReagentInformation
import com.example.reagent_management.ui.operatedialog.adapter.ReagentInfoAdapter
import com.example.reagent_management.ui.operatedialog.viewmodle.ReagentManagerViewModel
import com.hjq.toast.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ReagentManagerFragment : BaseDBDialogFragment() {

    private val mState: ReagentManagerViewModel by viewModels()
    private lateinit var job: Job

    override fun getDataBindingConfig(): DataBindingConfig? {
        val adapter = ReagentInfoAdapter()
        job = mState.getAllReagent("")
        mState.reagentMessages.observe(this) {
            lifecycleScope.launch {
                adapter.submitData(it)
            }
        }
        adapter.setOnItemClick(object : ItemClickListener{
            override fun itemClick(data: Any?) {
                data?.let {
                    (it as ReagentInformation).apply {
                        val updateReagent = ReagentInsertFragment()
                        updateReagent.updateData(this)
                        updateReagent.showNow(childFragmentManager,"updateReagent")
                    }
                }
            }

        })
        adapter.setOnItemChildClick(object :ItemChildCLickListener{
            override fun itemClick(viewId: Int, data: Any?) {
                if (viewId == R.id.delete){
                    ToastUtils.show("点击删除$data")
                }
            }
        })
        return DataBindingConfig(R.layout.fragment_reagent_manager)
            .addBindingParam(BR.adapter, adapter)
    }

    override fun initViewOrObserve() {
        setTransAndHeight(true, 0.5f)
        with(getBinding() as FragmentReagentManagerBinding) {
            back.setOnClickListener { dismissAllowingStateLoss() }
            search.setOnSearchClickListener { title.visibility = View.INVISIBLE }
            search.setOnCloseListener {
                title.visibility = View.VISIBLE
                false
            }
            //新增试剂
            ok.setOnClickListener {
                ReagentInsertFragment().showNow(childFragmentManager, "ReagentFragment")
            }
            lifecycleScope.launch {
                search.onQueryTextFlow()
                    .debounce(500)
                    .distinctUntilChanged()
                    .flowOn(Dispatchers.IO)
                    .collect {
                        if (job.isActive) job.cancel()
                        if (job.isCancelled) {
                            job = mState.getAllReagent(it)
                        }
                    }
            }
        }
    }
}